package com.symria.client.system;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;

import com.symria.system.constants.ATTSystem;
import com.symria.system.util.BaseAction;
import com.symria.system.util.SessionHelper;

public class SystemUtilAction extends BaseAction {
	   /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final int WIDTH = 65;
	   private final int HEIGHT = 20;
	   private ByteArrayInputStream inputStream;   
	    public String imgCheckCode() throws Exception{      
	    	char[] rands = this.generateCheckCode();
	    	BufferedImage image=createImg(rands);
	        ByteArrayOutputStream output = new ByteArrayOutputStream();   
	        ImageOutputStream imageOut = ImageIO.createImageOutputStream(output);   
	        ImageIO.write(image, "JPEG", imageOut);   
	        imageOut.close();   
	        ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());   
	        setInputStream(input);
	        //set session
	        SessionHelper.set(ATTSystem.SESSION_IMG_CODE, new String(rands));
	        return SUCCESS;   
	    }   
	    /*   
	     * 给定范围获得随机颜色   
	     */   
	    private Color getRandColor(int fc,int bc){   
	        Random random = new Random();   
	        if(fc>255) fc=255;   
	        if(bc>255) bc=255;   
	        int r=fc+random.nextInt(bc-fc);   
	        int g=fc+random.nextInt(bc-fc);   
	        int b=fc+random.nextInt(bc-fc);   
	        return new Color(r,g,b);   
	   }   
		private char[] generateCheckCode(){
			String chars = "0123456789";
			char[] rands = new char[4];
			for(int i=0;i<rands.length;i++){
				int rand = (int)(Math.random()*chars.length());
				rands[i] = chars.charAt(rand);
				
			}
			return rands;
		}
		public BufferedImage createImg(char[] rands){
			Random random = new Random();
			BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);  
             //获取图形上下文   
		     Graphics g = image.getGraphics(); 
             //设定背景色   
		     g.setColor(new Color(225,230,217));   
		     g.fillRect(0, 0, WIDTH, HEIGHT); 
		     g.setFont(new Font("Times New Roman",Font.PLAIN,18));  
		     g.setColor(getRandColor(160,200)); 
		     fillSpot(g,20);
		        for (int i=0;i<rands.length;i++ ){ 
		            g.setColor(new Color(64,74,68));     
		            g.drawString(""+rands[i],13*i+6,16);  
		        }   
		     g.dispose();
			 return image;
		}
		public void fillSpot(Graphics g,int num){
	        for (int i=0;i<num;i++){   
	         int x = (int) (Math.random()*WIDTH);   
	         int y = (int) (Math.random()*HEIGHT);   
	         int xl = (int)(Math.random()*12);   
	         int yl = (int)(Math.random()*12);     
	         g.drawLine(x,y,x+xl,y+yl);   
	        }
		}
	    public void setInputStream(ByteArrayInputStream inputStream) {   
	        this.inputStream = inputStream;   
	    }   
	    public ByteArrayInputStream getInputStream() {   
	        return inputStream;   
	    }   	
}
